package com.hxz.djbxkj.HxzExecutor.ExecutorCode.Run.Abstract;

import com.hxz.djbxkj.HxzExecutor.ExecutorCode.JobJk;
import com.hxz.djbxkj.HxzExecutor.Model.ParameterMode;
import com.hxz.djbxkj.HxzExecutor.annotation.HxzMethod;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public abstract class DoPlayMethodByHxzMethod implements GetMethodByClassJk<HxzMethod>,CkMethodByClassJk, DoPlayMethodByClassJk,Runnable {
    private List<Method> methods=null;

    public boolean CkJobJk(JobJk jk, String id){
        this.methods=this.getMethodByClass(jk, HxzMethod.class,id);
        if(methods.size()==0||methods.size()>1) return false;//throw new Exception("必须使用@HxzMethod注解修饰唯一一个函数");
        return true;
    }

    public Object DoPlay(JobJk jk, ParameterMode pm) throws Exception{
        return methods.get(0).invoke(jk,pm.getParameter());
    }

    public List<Method> getMethodByClass(JobJk jk, Class<HxzMethod> c,String id) {
        Method[] methods = jk.getClass().getDeclaredMethods();
        List<Method> ls=new ArrayList<Method>();
        for(Method m:methods){
            if(m.isAnnotationPresent(c))
                if(m.getAnnotation(c).id().equals(id))
                    ls.add(m);
        }
        return ls;
    }
}
